// pages/login/login.ts
var { request } = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPassword: true,
    isError: false,
    userName: '',
    code:''
  },
  //监听页面加载钩子
  onLoad() {
    setTimeout(()=>{
      // 登录
      wx.login({
        success: res => {
          console.log(res.code,'wx.login')
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          this.setData({
            code:res.code
          })
        },
        fail: res => {
          // 登录失败
          console.log(res,"登录失败！");
        }
      })
    },1000)
  },
  //失去焦点时获取用户名
  getValue(e: any) {
    console.log('失去焦点');
    this.setData({
      userName: e.detail.value
    })
  },
  handlePassword(e: any) {
    console.log(e);
    this.setData({
      isPassword: !this.data.isPassword
    })
  },
  formSubmit(e: any) {
    const userInfo = e.detail.value
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    wx.requestSubscribeMessage({
      tmplIds: ['AwE1KaE8hry5JrGCVYpExuw6yt-kFmbzOiVCv0h6Lu8'],
      success(res) {
        if (res['AwE1KaE8hry5JrGCVYpExuw6yt-kFmbzOiVCv0h6Lu8'] == 'accept') {
          console.log(res,'requestSubscribeMessage');
          request('/admin/employee/getOpenId', {
            method:'POST',
            data:{
              code:that.data.code
            }
          }).then((res: any) => {
            console.log(res,'getOpenId');
            // 用户同意订阅消息
            wx.showToast({
              title: '订阅成功',
              icon: 'success'
            });
          })
        } else {
          // 用户拒绝订阅消息
          wx.showToast({
            title: '取消订阅',
            icon: 'none'
          });
        }
      },
      fail(err) {
        console.error(err);
      }
    });
    //验证用户名和密码
    if (!userInfo.userName || !userInfo.passWord) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let that = this;
    //发起登录请求
    request('/admin/employee/login', {
      data: {
        username: userInfo.userName,
        password: userInfo.passWord
      },
      method:'POST'
    }).then((res: any) => {
      console.log(res,'login');
      if (res.data.code == 1) {
        let userInfo = res.data.data
        that.setData({
          isError: false
        })
        //登陆成功
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 2000,
        })
        wx.switchTab({
          url: '/pages/list/list'
        })
        //本地保存token值用于下次判断登录状态
        wx.setStorageSync('token', userInfo.token)
        wx.setStorageSync('role', userInfo.role)
        wx.setStorageSync('user', userInfo.id)
        setTimeout(() => {
        }, 800)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
        // that.setData({
        //   isError: true
        // })
      }
    })
  },
})